package hackerrank.algorithms.warmup;

import java.util.*;

/**
 * Created by Tzachi on 16/10/2014.
 *
 * Watson gives to Sherlock an array: A1,A2,⋯,AN. He also gives to Sherlock two other arrays: B1,B2,⋯,BM and C1,C2,⋯,CM.
 * Then Watson asks Sherlock to perform the following program:
 *
        for i = 1 to M do
            for j = 1 to N do
                if j % B[i] == 0 then
                    A[j] = A[j] * C[i]
                endif
            end do
        end do
 * Can you help Sherlock and tell him the resulting array A? You should print all the array elements modulo (10^9+7).
 *
 * Pay attention to Time Complexity - it is not brute force over the arrays
 */

public class SherlockAndQueries {

    private static Map<Long, Long> occurrencesMap = new HashMap<>();
    private static final long MOD = 1000000007;

    public static void preCalc(long[] a, long[] b, long[] c) {
        for (int i = 0; i < b.length; i++) {
            Long count = occurrencesMap.get(b[i]);
            if (count == null){
                count = c[i];
            } else {
                count = (count * c[i]) % MOD;
            }
            occurrencesMap.put(b[i], count);
        }
    }

    /**
     * @param a - length M
     * @param b - length N
     * @param c - length N
     * @return
     */
    public static long[] calcArray(long[] a, long[] b, long[] c) {
        for (int i = 1; i <= a.length; i++) {
            for (int j = 1; j * i <= a.length; j++) {
                if (occurrencesMap.get((long)i) != null) {
                    a[(i) * (j) - 1] = (a[(i) * (j) - 1] * occurrencesMap.get((long)i))%MOD;
                }
            }
        }
        return a;
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int aSize = in.nextInt();
        int bcSize = in.nextInt();
        long[] a = new long[aSize];
        for (int i = 0; i < aSize; i++) {
            a[i] = in.nextInt();
        }
        long[] b = new long[bcSize];
        long[] c = new long[bcSize];
        for (int i = 0; i < bcSize; i++) {
            b[i] = in.nextInt();
        }
        for (int i = 0; i < bcSize; i++) {
            c[i] = in.nextInt();
        }

        preCalc(a, b, c);
        for (long res : calcArray(a, b, c)) {
            System.out.print(res + " ");
        }
    }
}
